package com.fsm.system.serviceImpl;

import com.fsm.common.tools.Result.Result;
import com.fsm.system.entity.table.TSystemMenuTable;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.StringUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.fsm.system.entity.TSystemMenu;
import com.fsm.system.mapper.TSystemMenuMapper;
import com.fsm.system.service.TSystemMenuService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 *  菜单管理服务层实现。
 *
 * @author
 * @since
 */
@Service
public class TSystemMenuServiceImpl extends ServiceImpl<TSystemMenuMapper, TSystemMenu> implements TSystemMenuService {

    /**
     * 公共整合查询条件
     * @param tSystemMenu
     * @return
     */
    private static QueryWrapper getSelectQueryWrapper(TSystemMenu tSystemMenu){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtil.isNotBlank(tSystemMenu.getTitle())){
            queryWrapper.where(TSystemMenuTable.T_SYSTEM_MENU.TITLE.eq(tSystemMenu.getTitle()));
        }
        queryWrapper.orderBy(TSystemMenuTable.T_SYSTEM_MENU.ID.desc());
        return queryWrapper;
    }

    /**
     * 添加菜单管理
     *
     * @param tSystemMenu
     * @return
     */
    @Override
    @Transactional
    public Result addTSystemMenu(TSystemMenu tSystemMenu) throws Exception {
        int sign = this.getMapper().insert(tSystemMenu);
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("添加菜单管理失败");
        }
        return Result.ok();
    }

    /**
     * 删除菜单管理
     *
     * @param tSystemMenu
     * @return
     */
    @Transactional
    @Override
    public Result deleteTSystemMenu(TSystemMenu tSystemMenu) throws Exception {
        int sign = this.getMapper().deleteById(tSystemMenu.getId());
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("删除菜单管理失败");
        }
        return Result.ok();
    }

    /**
     * 更新菜单管理
     *
     * @param tSystemMenu
     * @return
     */
    @Transactional
    @Override
    public Result updateTSystemMenu(TSystemMenu tSystemMenu) throws Exception {
        int sign = this.getMapper().update(tSystemMenu);
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("更新菜单管理失败");
        }
        return Result.ok();
    }

    /**
     * 分页查询菜单管理
     *
     * @param tSystemMenu
     * @return
     */
    @Override
    public Result pageTSystemMenu(TSystemMenu tSystemMenu) throws Exception {
        QueryWrapper queryWrapper = getSelectQueryWrapper(tSystemMenu);
        Page<TSystemMenu> tSystemMenuPage = this.getMapper().paginateWithRelations(tSystemMenu.getPage(),tSystemMenu.getLimit(),queryWrapper);
        return Result.ok(tSystemMenuPage.getRecords(),tSystemMenuPage.getTotalRow());
    }

    /**
     * 查询菜单管理
     *
     * @param tSystemMenu
     * @return
     */
    @Override
    public Result getTSystemMenuList(TSystemMenu tSystemMenu) throws Exception{
        QueryWrapper queryWrapper = getSelectQueryWrapper(tSystemMenu);
        queryWrapper.where(TSystemMenuTable.T_SYSTEM_MENU.PID.isNull());
        List<TSystemMenu> tSystemMenuList = this.getMapper().selectListWithRelationsByQuery(queryWrapper);
        return Result.ok(tSystemMenuList,tSystemMenuList.size());
    }

    /**
     * 通过ID查询菜单管理
     *
     * @param tSystemMenu
     * @return
     */
    @Override
    public Result getTSystemMenuById(TSystemMenu tSystemMenu) throws Exception{
        TSystemMenu tSystemMenuOne = this.getMapper().selectOneWithRelationsById(tSystemMenu.getId());
        return Result.ok(tSystemMenuOne);
    }

}
